/*
 * Copyright (c) Jiandong.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import hilog from '@ohos.hilog';
import { MessageQueueThread } from './MessageQueueThread';
import { MessageQueueThreadImpl } from './MessageQueueThreadImpl';
import { MessageQueueThreadSpec } from './MessageQueueThreadSpec';
import HashMap from '@ohos.util.HashMap';
import { ReactQueueConfigurationSpec } from './ReactQueueConfigurationSpec';
import { ReactQueueConfiguration } from './ReactQueueConfiguration';
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

//package com.facebook.react.bridge.queue;
//
//import android.os.Looper;
//import com.facebook.react.common.MapBuilder;
//import java.util.Map;

export class ReactQueueConfigurationImpl implements ReactQueueConfiguration {
  private static readonly Tag = "React ReactQueueConfigurationImpl"
//  private readonly mUIQueueThread: MessageQueueThreadImpl;
//  private readonly mNativeModulesQueueThread: MessageQueueThreadImpl
//  private readonly mJSQueueThread: MessageQueueThreadImpl

  public constructor(
      private readonly mUIQueueThread: MessageQueueThreadImpl,
      private readonly mNativeModulesQueueThread: MessageQueueThreadImpl,
      private readonly mJSQueueThread: MessageQueueThreadImpl) {
//    mUIQueueThread = uiQueueThread;
//    mNativeModulesQueueThread = nativeModulesQueueThread;
//    mJSQueueThread = jsQueueThread;
  }

  public getUIQueueThread(): MessageQueueThread {
    return this.mUIQueueThread;
  }

  public getNativeModulesQueueThread() {
    return this.mNativeModulesQueueThread;
  }

  public getJSQueueThread() {
    return this.mJSQueueThread;
  }

  /**
   * Should be called when the corresponding {@link com.facebook.react.bridge.CatalystInstance} is
   * destroyed so that we shut down the proper queue threads.
   */
  public destroy() {
//    if (mNativeModulesQueueThread.getLooper() != Looper.getMainLooper()) {
//      mNativeModulesQueueThread.quitSynchronous();
//    }
//    if (mJSQueueThread.getLooper() != Looper.getMainLooper()) {
//      mJSQueueThread.quitSynchronous();
//    }
  }

  public static create(
        spec: ReactQueueConfigurationSpec /*, exceptionHandler: QueueThreadExceptionHandler*/): ReactQueueConfigurationImpl {
    hilog.info(0x0000, ReactQueueConfigurationImpl.Tag, '%{public}s', "ReactThread create all");

    //todo :useless
//    let specsToThreads: HashMap<MessageQueueThreadSpec, MessageQueueThreadImpl>  = new HashMap<MessageQueueThreadSpec, MessageQueueThreadImpl>();

    let uiThreadSpec = MessageQueueThreadSpec.mainThreadSpec();
    let uiThread = MessageQueueThreadImpl.create(uiThreadSpec/*, exceptionHandler*/);
//    specsToThreads.set(uiThreadSpec, uiThread);

//    let jsThread = specsToThreads.get(spec.getJSQueueThreadSpec());
//    if (jsThread == null) {
      let jsThread = MessageQueueThreadImpl.create(spec.getJSQueueThreadSpec() /*, exceptionHandler*/);
//    }

//    let nativeModulesThread: MessageQueueThreadImpl =
//        specsToThreads.get(spec.getNativeModulesQueueThreadSpec());
//    if (nativeModulesThread == null) {
      let nativeModulesThread =
          MessageQueueThreadImpl.create(spec.getNativeModulesQueueThreadSpec()/*, exceptionHandler*/);
//    }

    return new ReactQueueConfigurationImpl(uiThread, nativeModulesThread, jsThread);
  }
}
